<?php

class ShenQiAction extends Action {
	//获取
	public function getList($isRed=null,$status=null,$deleted=null,$order="a.updateTime", $orderBy="desc", $start = 0, $limit = 25) {				
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid=C("PLANT_CUSTOMERID");
		Session::switchCustomer(0);
		$MiModel = M("miuser_share");
		Session::switchCustomer($cid);
		if($isRed!=="" &&  $isRed!=null) {
			$where["a.isRed"]=array('eq',$isRed);
		}
		if($status!=="" &&  $status!=null) {
			$where["a.status"]=array('eq',$status);
		}
		if($deleted!=="" && $deleted!=null) {
			$where["a.deleted"]=array('eq',$deleted);
		}
		$arr = $MiModel
				->alias("a")
				->join("wx_all_user b on a.uId=b.id")
				->join("wx_quan_user c on c.qid=a.qId and c.uid=a.uId")
				->join("wx_".$cid."_user_weixin_info d on d.id=b.weixinUserId")
				->join("wx_miyouquan q ON q.id=c.qid")
        		->field("a.*,q.name as qName,b.fullName,d.nickname,d.headimgurl")
        		->where($where)	
        		->order("$order $orderBy")->limit("$start, $limit")->select();

      if ($arr) {
		    
			$count = $MiModel
		        ->alias("a")
				->join("wx_all_user b on a.uId=b.id")
				->join("wx_quan_user c on c.qid=a.qId and c.uid=a.uId")
				->join("wx_".$cid."_user_weixin_info d on d.id=b.weixinUserId")
				->join("wx_miyouquan q ON q.id=c.qid")
		->where($where)
		->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
		
		
	}
	public function getDetail($id, $start = 0, $limit = 25) {
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid=C("PLANT_CUSTOMERID");
		Session::switchCustomer(0);
		$MiModel = M("miusershare_getred");
		Session::switchCustomer($cid);
		$where["a.usId"]=array('eq',$id);
		$arr = $MiModel
				->alias("a")
				->join("wx_miuser_share b on a.usId=b.id")
				->join("wx_all_user user on a.uId=user.id")
				->join("wx_".$cid."_user_weixin_info d on d.id=user.weixinUserId")
        		->field("a.*,d.nickname,d.headimgurl")
        		->where($where)	
        		->order("a.createTime desc")->limit("$start, $limit")->select();
      if ($arr) {
		    
			$count = $MiModel
		        ->alias("a")
				->join("wx_miuser_share b on a.usId=b.id")
				->join("wx_all_user user on b.uId=user.id")
				->join("wx_".$cid."_user_weixin_info d on d.id=user.weixinUserId")
		->where($where)
		->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
	/**
	 * [saveShare 活动账户返现]
	 * @param  [type] $param [description]
	 * @return [type]        [description]
	 */
	public function accountToMoney($mintotal=20,$startTime,$endTime,$type="query") {
	    $customerId = C("PLANT_CUSTOMERID");
	    Session::switchCustomer(0);
		$MiUserShareModel = M("miuser_share");
		$miAccountModel = M("miaccount_friend");
		Session::switchCustomer($customerId);
		$time=date("Y-m-d H:i:s");

		$shareList=$MiUserShareModel->query("select * from wx_miuser_share where total>=$mintotal and status=1 
			and createTime>='$startTime' and createTime<='$endTime' and subsidy <> 1");

		print_r($MiUserShareModel->getlastsql());
		print_r($shareList);
		
		if($type=="query"){
			return;
		}
		if($type=="update") {
			foreach ($shareList as $key => $value) {
				//扣除账户余额添加红包记录
				$total=$value["total"];
				$MiUserShareModel->startTrans();
				$uid=$value["uId"];
				$qId=$value["qId"];
				$id=$value["id"];
				$success1=$MiUserShareModel->query("update wx_miuser_share set subsidy=1 where id=$id");
				if($success1===false) {
					$MiUserShareModel->rollback();
					Output::instance()->resultCode(code_sql_error);
					return;
				}
				$success2=$MiUserShareModel->query("update wx_quan_user set miCoins=miCoins+".$total." where uid=$uid and qid=$qId");
				if($success2===false) {
					$MiUserShareModel->rollback();
					Output::instance()->resultCode(code_sql_error);
					return;
				}
				//添加支出记录
				$addData1=array(
					"module"=>"shenqi",
					"moduleId"=>$value["id"],
					"uId"=>$uid,
					"quanId"=>$qId,
					"vId"=>0,
					"description"=>"神器活动返现",
					"sId"=>0,
					"vsistId"=>0,
					"money"=>$total,
					"type"=>0,
					"createTime"=>date("Y-m-d H:i:s"),
					"deleted"=>0
				);
				$success3=$miAccountModel->add($addData1);
				if($success3===false) {
					$MiUserShareModel->rollback();
					Output::instance()->resultCode(code_sql_error);
					return;
				}
				print_r("success");
				$MiUserShareModel->commit();
			}
		}
	}		
}
